//
//  MYGUploadDownloadView.swift
//  MYDoctor
//
//  Created by Denny on 2024/4/15.
//

import UIKit

class MYGUploadDownloadView: UIView {
    var tapCloseCompletion:(()->Void)?
    
    var titleLB: UILabel = {
        let cotnrol = UILabel()
        cotnrol.text = "传输列表"
        cotnrol.font = UIFont.regular(18)
        cotnrol.textColor = .black
        return cotnrol
    }()
    
    lazy var popBackBtn: UIButton = {
        let control = UIButton(type: .custom)
        control.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        control.setImage(UIImage(named: "comonback"), for: .normal)
        control.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        return control
    }()
    var uploadView: MYGUploadView = {
        let control = MYGUploadView()
        control.isHidden = true
        return control
    }()
    
    var downloadView: MYGDownloadView = {
        let control = MYGDownloadView()
        control.isHidden = true
        return control
    }()
    
    lazy var uploadBtn: UIButton = {
        let control = UIButton()
        control.setTitle("上传", for: .normal)
        control.addTarget(self, action: #selector(tapUploadDownload(_ :)), for: .touchUpInside)
        control.tag = 50
        control.titleLabel?.font = UIFont.regular(13)
        return control
    }()
    
    lazy var downloadBtn: UIButton = {
        let control = UIButton()
        control.addTarget(self, action: #selector(tapUploadDownload), for: .touchUpInside)
        control.setTitle("下载", for: .normal)
        control.tag = 51
        control.titleLabel?.font = UIFont.regular(13)
        return control
    }()
    
    var line: UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hexString: "#205CFF").withAlphaComponent(0.7)
        return control
    }()
    
    @objc func tapUploadDownload(_ sender: UIButton) {
        UIView.animate(withDuration: 0.25) {
            self.line.snp.remakeConstraints { make in
                make.size.equalTo(CGSize(width: 16, height: 3))
                make.centerX.equalTo(sender.snp.centerX)
                make.centerY.equalTo(sender.snp.centerY).offset(13)
            }
            self.layoutIfNeeded()
        }
      
        uploadView.isHidden = sender.tag == 51
        downloadView.isHidden = sender.tag == 50
        if sender.tag == 50 {
            self.uploadBtn.setTitleColor(UIColor.init(hexString: "#205CFF"), for: .normal)
            self.downloadBtn.setTitleColor(UIColor.init(hexString: "##767676"), for: .normal)
        }else {
            self.downloadBtn.setTitleColor(UIColor.init(hexString: "#205CFF"), for: .normal)
            self.uploadBtn.setTitleColor(UIColor.init(hexString: "##767676"), for: .normal)
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(titleLB)
        titleLB.snp.makeConstraints { make in
            make.top.equalTo(statusBarHeight)
            make.centerX.equalToSuperview()
        }
        addSubview(popBackBtn)
        backgroundColor = .white
        popBackBtn.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.size.equalTo(CGSize.init(width: 60, height: 60))
            make.top.equalTo(statusBarHeight)
        }
        addSubview(uploadBtn)
        uploadBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize.init(width: 70, height: 40))
            make.left.equalToSuperview()
            make.top.equalTo(popBackBtn.snp.bottom)
        }
        addSubview(downloadBtn)
        downloadBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize.init(width: 70, height: 40))
            make.top.equalTo(popBackBtn.snp.bottom)
            make.left.equalTo(uploadBtn.snp.right)
            
        }
        
        addSubview(line)
        line.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 16, height: 3))
            make.centerX.equalTo(uploadBtn.snp.centerX)
            make.centerY.equalTo(uploadBtn.snp.centerY).offset(13)
        }
        addSubview(uploadView)
        uploadView.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom)
            make.left.right.bottom.equalToSuperview()
            
        }
        addSubview(downloadView)
        downloadView.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        tapUploadDownload(self.uploadBtn)
    }
    
    @objc func backAction() {
        if let cc = self.tapCloseCompletion  {
            cc()
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
